<template>
  <div class="wrapper">
    <img class="wrapper__img" src="http://www.dell-lee.com/imgs/vue3/user.png"/>
    <div class="wrapper__input">
      <input v-model="data.username" class="wrapper__input__content" placeholder="请输入手机号" />
    </div>
    <div class="wrapper__input">
      <input v-model="data.password" class="wrapper__input__content"  placeholder="请输入密码" />
    </div>
    <div class="wrapper__login-button" @click="handleLogin">登陆</div>
    <div class="wrapper__login-link" @click="handleRegister">立即注册</div>
    <Toast v-if="toastData.showToast" :message="toastData.toastMessage"/>
  </div>
</template>

<script>
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import { post } from '../../utils/request'
import Toast, { useToastEffect } from '../../components/Toast'

const userLoginEffect = (showToast)=>{
  const router = useRouter();
  const data = reactive(
    {
      username: '',
      password: '',
    }
  )
  const handleLogin = async () => {
    try {
      const result = await post('/api/user/login' , {
        username: data.username,
        password: data.password,
      })
      if(result ?.errno === 0){
        localStorage.setItem('isLogin', true)
        router.push({name: 'Home'});
      }else {
        showToast('登录失败111')
      }
    }catch (e){
      showToast('登录失败222')
    }

  }

  return {handleLogin, data}
}

export default {
  name: 'Login',
  components: { Toast },
  // setup职责就是告诉我们：当前代码执行的一个流程，
  // 而每个流程里面的业务建议独立到对应的Effect函数里面去实现，
  // 这样可以有效的避免setup函数过大过杂的问题
  setup() {
    const router = useRouter();
    const { showToast, toastData } = useToastEffect();
    const { handleLogin, data } = userLoginEffect(showToast);

    const handleRegister = ()=>{
      router.push({name: 'Register'});
    };

    return {
      data,
      handleLogin,
      handleRegister,
      showToast, toastData
    }
  }
}
</script>

<style lang="scss" scoped>
@import '../../style/viriables.scss';
.wrapper {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  &__img {
    display: block;
    margin: 0 auto .4rem auto;
    width: .66rem;
    height: .66rem;
  }
  &__input {
    height: .48rem;
    margin: 0 .4rem .16rem .4rem;
    padding: 0 .16rem;
    background: #F9F9F9;
    border: 1px solid rgba(0,0,0,0.10);
    border-radius: 6px;
    border-radius: 6px;
    &__content {
      line-height: .48rem;
      border: none;
      outline: none;
      width: 100%;
      background: none;
      font-size: .16rem;
      color: $content-notice-fontcolor;
      &::placeholder {
        color: $content-notice-fontcolor;
      }
    }
  }
  &__login-button {
    margin: .32rem .4rem .16rem .4rem;
    line-height: .48rem;
    background: #0091FF;
    box-shadow: 0 .04rem .08rem 0 rgba(0,145,255,0.32);
    border-radius: .04rem;
    border-radius: .04rem;
    color: #fff;
    font-size: .16rem;
    text-align: center;
  }
  &__login-link {
    text-align: center;
    font-size: .14rem;
    color: $content-notice-fontcolor;
  }
}
</style>
